import { Body, Controller, Get, Inject, Post, Put, Query } from '@midwayjs/core';
import { JwtMiddleware } from '@/middleware/JwtMiddleware';
import { AddReportReq, UpdateReportReq } from '@/dto/house/ReportDto';
import { ReportService } from '@/service/house/ReportService';
import { ResultUtils } from '@/common/ResultUtils';
import { GetReportReq } from '@/dto/user/AdminDto';

@Controller('/report')
export class ReportController {
  @Inject()
  private reportService: ReportService;

  /**
   * 添加维修记录
   * @param addReportReq
   */
  @Post('/', {middleware: [JwtMiddleware]})
  async addReport(@Body() addReportReq: AddReportReq) {
    return new ResultUtils().success(await this.reportService.addReport(addReportReq));
  }

  /**
   * 通过租客id查询房屋维修记录
   * @param tenantId
   */
  @Get('/tenant', {middleware: [JwtMiddleware]})
  async getReportByTenantId(@Query('tenantId') tenantId: number) {
    return new ResultUtils().success(await this.reportService.getReportByTenantId(tenantId));
  }

  /**
   * 通过房东id查询房屋维修记录
   * @param landlordId
   */
  @Get('/landlord', {middleware: [JwtMiddleware]})
  async getReportByLandlordId(@Query('landlordId') landlordId: number) {
    return new ResultUtils().success(await this.reportService.getReportByLandlordId(landlordId));
  }

  /**
   * 更新维修状态
   */
  @Put('/', {middleware: [JwtMiddleware]})
  async updateReportStatusById(@Body() updateObj: UpdateReportReq) {
    const {reportId, status} = updateObj;
    return new ResultUtils().success(await this.reportService.updateReportStatus(reportId, status));
  }

  /**
   * 管理员获取房屋维修信息
   */
  @Post('/admin/get', {middleware: [JwtMiddleware]})
  async getReportByAdmin(@Body() getReportReq: GetReportReq) {
    Object.keys(getReportReq).forEach(key => {
      if (!getReportReq[key] && getReportReq[key] !== 'status' && getReportReq[key] !== 0) {
        delete getReportReq[key];
      }
    });
    return new ResultUtils().success(await this.reportService.getReportByAdmin(getReportReq));
  }
}